package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogain;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @GetMapping("/detail")
    public Object viewnumTop3(String id) {
        //根据攻略的id查出该攻略的所有信息
        Strategy strategy = strategyService.get(id);
        //调用服务,使阅读量+1
        strategyStatisVORedisService.viewnumIncrease(id,1);
        //返回
        JsonResult success = JsonResult.success();
        success.setData(strategy);
        return success;
    }

    @GetMapping("/query")
    public Object query(String destId, @ModelAttribute("qo") StrategyQuery qo) {
        //获得分页对象
        Page<Strategy> page = strategyService.query(qo);
        JsonResult success = JsonResult.success();
        success.setData(page);
        return success;
    }


    @GetMapping("/themes")
    public Object themes() {
        //查询出所有的攻略主题
        List<StrategyTheme> strategyThemes = strategyThemeService.selectAll();
        return JsonResult.success(strategyThemes);
    }

    //添加评论
    @RequireLogain
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment,@UserParam UserInfo userInfo) {
        //将用户的数据注入
        BeanUtils.copyProperties(userInfo, strategyComment);
        //userId
        strategyComment.setUserId(userInfo.getId());
        //调用服务层方法进行评论的添加
        strategyCommentService.addComment(strategyComment);
        //调用redis服务层方法,进行评论数的添加
        strategyStatisVORedisService.addCommentNum(strategyComment.getStrategyId(),1);
        return JsonResult.success();
    }


    @GetMapping("/comments")
    public Object comments(StrategyCommentQuery qo, @UserParam UserInfo userInfo) {
        //查询所有评论,需要过滤分页
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }


    @RequireLogain
    @PostMapping("/commentThumb")   //点赞
    public Object commentThumb(String cid,@UserParam UserInfo userInfo) {
        //cid   评论id    sid 当前攻略id
        //调用服务层方法,进行点赞与取消点赞操作
        strategyCommentService.commentThumb(cid,userInfo.getId());
        return JsonResult.success();
    }


    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        //根据攻略id,从缓存中拿对应的StrategyStaticVO对象
        StrategyStatisVO vo = strategyStatisVORedisService.getVo(sid);
        return JsonResult.success(vo);
    }


    @RequireLogain
    @PostMapping("/favor")
    public Object favor(String sid,@UserParam UserInfo userInfo) {
        //调用服务层方法
        boolean flag = strategyStatisVORedisService.favor(sid,userInfo.getId());
        return JsonResult.success(flag);
    }

    //要求用户登录
    @RequireLogain
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo) {
        //调用服务层方法
        boolean flag = strategyStatisVORedisService.strategyThumbup(sid,userInfo.getId());
        return JsonResult.success(flag);
    }



}
